package com.comtime.swdevice;

import java.util.ArrayList;
import java.util.List;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothAdapter.LeScanCallback;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;

public class SWDeviceScanManager {

	private BluetoothAdapter bluetoothAdapter;
	private BluetoothManager bluetoothManager;
	private Context context;
	private DeviceScanCallBack scanCallBack;

	private BluetoothStateListener bluetoothStateListener;

	/**
	 * 
	 * @param context
	 */
	public SWDeviceScanManager(Context context) {
		this.context = context;
		init();

	}

	public void setBluetoothStateListener(
			BluetoothStateListener bluetoothStateListener) {
		this.bluetoothStateListener = bluetoothStateListener;
	}

	/**
	 * 
	 * @param scanCallBack
	 */
	public void setScanCallBack(DeviceScanCallBack scanCallBack) {
		this.scanCallBack = scanCallBack;
	}

	/**
	 * 
	 * @return the phone is supportBle or not
	 */
	public boolean supportBle() {
		if (context.getPackageManager().hasSystemFeature(
				PackageManager.FEATURE_BLUETOOTH_LE)) {
			return true;
		}
		return false;
	}

	private void init() {
		bluetoothManager = (BluetoothManager) context
				.getSystemService(Context.BLUETOOTH_SERVICE);
		bluetoothAdapter = bluetoothManager.getAdapter();
		if (!bluetoothAdapter.isEnabled()) {
			bluetoothAdapter.enable();
		}

		IntentFilter filter = new IntentFilter();
		filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
		context.registerReceiver(receiver, filter);
	}

	public void enableBluetooth() {
		if (bluetoothAdapter != null) {
			bluetoothAdapter.enable();
		}
	}

	public void disableBluetooth() {
		if (bluetoothAdapter != null) {
			bluetoothAdapter.disable();
		}
	}

	public BluetoothDevice retrieveBluetoothDeviceWithMac(String mac) {
		if (bluetoothAdapter != null) {
			if (BluetoothAdapter.checkBluetoothAddress(mac)) {
				return bluetoothAdapter.getRemoteDevice(mac);
			}
		}
		return null;
	}

	public List<BluetoothDevice> getConnectedDevices() {

		List<BluetoothDevice> devices = new ArrayList<BluetoothDevice>();
		if (bluetoothManager != null) {
			for (BluetoothDevice bluetoothDevice : bluetoothManager
					.getConnectedDevices(BluetoothProfile.GATT)) {
				if (bluetoothDevice.getName() != null
						&& (bluetoothDevice.getName().contains("SmartWallit")
								|| bluetoothDevice.getName().contains(
										"SmartKee") || bluetoothDevice
								.getName().contains("SafeWallet"))) {
					devices.add(bluetoothDevice);
				}
			}
		}

		return devices;
	}

	public boolean bluetoothIsEnable() {
		return bluetoothAdapter.isEnabled();
	}

	/**
	 * start scan devices, the result will be on DeviceScanCallBack.
	 * onLeScan(BluetoothDevice device, final int rssi, byte[] scanRecord);
	 */

	public void startScan() {
		// UUID[] serviceUuids = new UUID[] { UUID
		// .fromString(UUIDUtils.WRITE_SERVICE_UUID) };
		// bluetoothAdapter.startLeScan(serviceUuids, callback);
		
		bluetoothAdapter.stopLeScan(callback);
		bluetoothAdapter.startLeScan(callback);

	}

	/**
	 * stopScan
	 */
	public void stopScan() {
		bluetoothAdapter.stopLeScan(callback);
	}

	private LeScanCallback callback = new LeScanCallback() {

		@Override
		public void onLeScan(BluetoothDevice device, final int rssi,
				byte[] scanRecord) {
			scanCallBack.onLeScan(device, rssi, scanRecord);
		}
	};

	public interface BluetoothStateListener {
		public void onBluetoothEnable();

		public void onBluetoothDisnable();

	}

	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction()
					.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {

				int state = intent.getExtras().getInt(
						BluetoothAdapter.EXTRA_STATE);
				if (state == BluetoothAdapter.STATE_ON) {
					if (bluetoothStateListener != null) {
						bluetoothStateListener.onBluetoothEnable();
					}
				} else if (state == BluetoothAdapter.STATE_OFF) {
					if (bluetoothStateListener != null) {
						bluetoothStateListener.onBluetoothDisnable();
					}
				}
			}
		}
	};

}
